﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ITAP.ShprossesBuilder.Modelling.Components.Actions;
using ITAP.ShprossesBuilder.Modelling.Components;
using ITAP.ShprossesBuilder.Editor;
using System.Windows.Forms;
using ITAP.ShprossesBuilder.Properties;
using System.Drawing;

namespace ITAP.ShprossesBuilder.Operations
{
    public class OperationAlignSelectedShprosses : IOperation
    {
        private ModelControl modelControl;
        public OperationAlignSelectedShprosses()
        {
        }

        #region IOperation Members

        public void Execute(ModelControl modelControl)
        {
            this.modelControl = modelControl;
            ContextMenuStrip menuStrip = new ContextMenuStrip();

            EventHandler handler = new EventHandler(OnItemClick);
            ToolStripItem horizontal = new ToolStripMenuItem("По вертикали", Resources.Horizontal.ToBitmap(), handler);
            horizontal.Tag = ComponentAlign.Horizontal;
            menuStrip.Items.Add(horizontal);

            //ToolStripItem _horizontal = new ToolStripMenuItem("По горизонтали (двигать присоединённые компоненты)", Resources.Horizontal.ToBitmap(), handler);
            //_horizontal.Tag = ComponentAlign.Horizontal;
            //menuStrip.Items.Add(_horizontal);


            ToolStripItem vertical = new ToolStripMenuItem("По горизонтали", Resources.Vertical.ToBitmap(), handler);
            vertical.Tag = ComponentAlign.Vertical;
            menuStrip.Items.Add(vertical);

            //ToolStripItem _vertical = new ToolStripMenuItem("По вертикали (двигать присоединённые компоненты)", Resources.Vertical.ToBitmap(), handler);
            //_vertical.Tag = ComponentAlign.Vertical;
            //menuStrip.Items.Add(_vertical);


            menuStrip.Show(Cursor.Position);             





            //AlignComponentsForm alignForm = new AlignComponentsForm(true);
            //if (alignForm.ShowDialog() == DialogResult.OK)
            //{
            //    IComponentAction alignShprosses = new ActionAlignSelectedShprosses(modelControl, alignForm.Align, alignForm.MoveConnectedComponents);
            //    foreach (IComponent selectedComponent in modelControl.ModelContainer.SelectedComponentsStack)
            //        if (selectedComponent is Shpross)
            //        {
            //            alignShprosses.Action(selectedComponent);
            //            break;
            //        }
            //}
        }

        #endregion

        #region IOperation Members

        public string Name
        {
            get { return "Выравнивание профилей"; }
        }

        #endregion

        #region IOperation Members


        public OperationGroup OperationGroup
        {
            get { return OperationGroup.ShprossOperation; }
        }

        #endregion

        #region IOperation Members


        public System.Drawing.Image Thumbnail
        {
            get { return Resources.AlignShprosses.ToBitmap(); }
        }

        #endregion

        #region IOperation Members


        public bool CanUndo
        {
            get { return true; }
        }

        #endregion

        private void OnItemClick(object sender, EventArgs e)
        {
            ToolStripMenuItem item = sender as ToolStripMenuItem;
            ComponentAlign align = (ComponentAlign)item.Tag;

            //RectangleF activeModelBoundingBox;
            //if (this.modelControl.ModelContainer.ActiveCell != null)
            //    activeModelBoundingBox = (this.modelControl.ModelContainer.ActiveCell as ModelCell).Model.BoundingBox;
            //else
            //    return;

            foreach (IComponent selectedComponent in this.modelControl.ModelContainer.SelectedComponentsStack)
                if (selectedComponent is Shpross)
                {
                    IComponentAction alignAction = new ActionAlignSelectedShprosses(this.modelControl, align, false);
                    alignAction.Action(selectedComponent);
                    break;
                }
            this.modelControl.UpdateModel();
        }

    }
}
